module.exports = ['$scope','$http', function ($scope,$http) {
    var _API = require('_API')($http);

    $scope.type = [
        {id:0,name:"普通",},
        {id:1,name:"计算公式1",},
        {id:2,name:"计算公式2",},
        {id:9,name:"管理",},
    ];

    //获取所有门店信息
    function refresh() {
        _API.shop.all().success(function (res) {
            $scope.shop_all = res;
            $scope.shop = {};
            $scope.isEdit = false;
        });
    }
    refresh();

    //门店信息列表click事件
    $scope.shop_list = function (list, data) {
        $scope.isEdit = true;
        for(var i in data){
            data[i].isActive = false;
        };
        list.isActive = true;

        _API.shop.get(list.id).success(function(res){
            $scope.shop = res;
            $scope.shop.type = Number(res.type);
        });

    };

    // 删除门店
    $scope.del_shop = function (shop,e) {
        e.stopPropagation();
        swal({
                title: "<small>确认删除门店 " + shop.name + " 吗?</small>",
                html:true,
                showCancelButton: true,
                closeOnConfirm: false,
                showLoaderOnConfirm: true,
                confirmButtonText:"确定",
                cancelButtonText:"取消",
                confirmButtonColor:"#3c6",
                animation:false,
            },
            function(){
                _API.shop.del(shop.id).success(function(){
                    swal.close();
                    refresh();
                    showToast('删除成功','ok');
                });
            });

    };

    // 添加门店
    $scope.addShop = function () {
        //切换右边标题文字
        $scope.isEdit = false;
        //不选中
        for(var i in $scope.shop){
            $scope.shop[i].isActive = false;
        };
        $('.input-uid').focus();
        $scope.shop = {};
    };

    // 门店信息提交||修改
    $scope.shop_submit = function () {
        var msg = {};
        if($scope.shop.id) msg.id = $scope.shop.id;
        if(!$scope.shop.name){
            showToast('名称不能为空','err');
            return;
        }else{
            msg.name = $scope.shop.name;
        }
        if($scope.shop.type == undefined){
            showToast('请选择类型','err');
            return;
        }else{
            msg.type = Number($scope.shop.type);
        }
        if($scope.shop.full_name) msg.full_name = $scope.shop.full_name;
        if($scope.shop.desc) msg.desc = $scope.shop.desc;
        // if($scope.shop.type) msg.type = $scope.shop.type;
        console.log(msg);
        if($scope.isEdit){
            //修改
            _API.shop.upd(msg).success(function(){
                refresh();
                showToast('修改门店成功','ok');
            });
        }else{
            // 新增
            _API.shop.add(msg).success(function(){
                refresh();
                showToast('添加门店成功','ok');
            });
        }

    };

}];
